angular.module('payPcApp')
    .factory('loginservice', ['$http', 'AppConfig', '$location', function($http, AppConfig, $location) {
        var data = angular.isDefined(data) ? data : {};

        var getolddata = function() {
            return this.data;
        };
        var getdata = function(param) {
            var data = 'username=' + param.username + '&password=' + param.password
            console.log(data);
            $http({
                method: 'post',
                url: 'http://benoob.com/weiphp2.0/index.php?s=/Home/User/loginout.html',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
                },
                data: data
            }).then(function(response) {
                console.log(response);
                /*                if (response.data.message == 'success' && response.data.code == '0') {
                                    AppConfig.data = response.data;
                                    _.each(response.data, function(v, k) {
                                        var key = 'APP_' + k;
                                        localStorage[key] = v;
                                    })
                                    AppConfig.universityId = 'gh_0b2f868ef759';
                                    $location.path('/history');
                                }*/
                $location.path('/bookSearch');
                return response;
            }, function(error) {
                console.log(error);
                return error;
            }, function(progress) {
                return progress;
            })
        };

        var updatedata = function() {
            var data = "grant_type=refresh_token&refresh_token=" + AppConfig.get_refresh_token() + "&client_id=sdjf";
            $http({
                method: 'post',
                url: 'http://120.26.48.150:82/Geese.HouQinBao.Auth/Token',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: data
            }).then(function(response) {
                console.log(response);
                response.data['.expires'] = Math.round(new Date(response.data['.expires']).getTime() / 1000);
                AppConfig.data = response.data;
                _.each(response.data, function(v, k) {
                    var key = 'APP_' + k;
                    localStorage[key] = v;
                })
                AppConfig.universityId = 'gh_0b2f868ef759';
                return response;
            }, function(error) {
                console.log(error);
                return error;
            }, function(progress) {
                return progress;
            })
        };
        return {
            data: this.data,
            getolddata: getolddata,
            getPrivate: getdata,
            updatedata: updatedata
        };
    }])
    .factory('AuthService', function(AppConfig) {
        var hasAuth = function(routename) {
            console.log(routename);
            return true;
        }
        return {
            hasAuth: hasAuth
        }
    });
